apiready = function() {
seajs.use(["../../api", "../../jquery", "../../app",'../../service'], function($api, $, app,service) {
    var vm = {
        el: '#app',
        data: {
          goodsList:[],
          page:1,
          offon:true,
          isFj:false,
          lng:"",
          lat:"",
          type:2,//2全部 4附近
        },
        mounted: function() {
          var that = this
            this.init()
            app.refreshHeaderInfo(function() {
                that.init();
            });
        },
        methods: {
            init: function() {
                var that = this;
                this.offon = true
                this.page = 1
                this.getHomeShopGoods()
            },
            // 触底
            scrolltobottom:function(){
              if(this.offon){
                this.offon = false
                this.page+=1
                this.getHomeShopGoods()
              }
            },
            // 设置附近
            setFj:function(){
              var that = this
              var bMap = api.require('bMap');
              if(this.isFj){//全部
                that.toHead()
                that.isFj = false
                that.page = 1
                that.type = 2
                that.lng = ""
                that.lat = ""
                that.getHomeShopGoods()
              }else{//附近
                bMap.getLocation({
                    accuracy: '100m',
                    autoStop: true,
                    filter: 1
                }, function(ret, err) {
                    if (ret.status) {
                        that.toHead()
                        that.isFj = true
                        that.page = 1
                        that.type = 4
                        that.lng = ret.lon
                        that.lat = ret.lat
                        that.getHomeShopGoods()
                    } else {
                        api.toast({
                            msg: '您还没有开启定位',
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                });
              }
            },
            // 获取商品信息
            getHomeShopGoods:function(){
              var that = this
              app.ajax(
                  service['getHomeShopGoods'], {
                      data: {
                          values: {
                            type:this.type,
                            page:this.page,
                            lng:this.lng,
                            lat:this.lat
                          }
                      }
                  },
                  function(res) {
                      if (res.code == 0) {
                        if(that.page==1){
                            that.goodsList = res.data
                        }else{
                          if(res.data.length==0){
                            api.toast({
                                msg: '没有更多数据...',
                                duration: 2000,
                                location: 'bottom'
                            });
                          }else{
                            that.goodsList = that.goodsList.concat(res.data)
                            that.offon = true
                          }
                        }
                      }
                  })
            },
            // 前往搜索
            toSearch:function(){
              api.openWin({
                  name: 'search',
                  url: '../search/search.html'
              });

            },
            // 前往商品详情
            toGoods_detail:function(goods_id){
              api.openWin({
                  name: 'goods_detail',
                  url: '../buy_goods/goods.html',
                  pageParam: {
                      goodsId: goods_id,
                      isSeller:0
                  },
                  reload:true
              });
            },
            //前往购物车
            toCart: function(){
              app.openWin('buyCart',{},{reload: true})
            },
            // 回到顶部
            toHead:function(){
              api.pageUp({
                  top: true
              })
            },
        }
    }
    var even = function() {
      api.addEventListener({
          name:'scrolltobottom',
          extra:{
              threshold:0
          }
      }, function(ret, err){
          newVue.scrolltobottom()
      });
    }
    var newVue = new Vue(vm);
    even();
});

}
